import * as usersService from '../services/users';

export default {
  namespace: 'users',
  state: {
    items: [],
    total: 0,
    page: 1,
    profile: null,
    roles: [],
    loginedUser: null,
  },
  reducers: {
    save(
      state,
      {
        payload: { items, total, page },
      }
    ) {
      return { ...state, items, total, page };
    },
    saveProfile(
      state,
      {
        payload: { profile },
      }
    ) {
      return { ...state, profile };
    },
    saveLoginedUser(
      state,
      {
        payload: { loginedUser },
      }
    ) {
      const newS = { ...state, loginedUser };
      console.log('login user state: ', newS);
      return newS;
    },
    saveRoles(
      state,
      {
        payload: { roles },
      }
    ) {
      return { ...state, roles };
    },
  },
  effects: {
    *fetchList(
      {
        payload: { page = 1, ...rest },
      },
      { call, put }
    ) {
      const { data } = yield call(usersService.fetchList, { page, ...rest });
      const { items, total } = data;
      yield put({
        type: 'save',
        payload: {
          ...rest,
          items,
          total,
          page: parseInt(page, 10),
        },
      });
    },
    *fetch({ payload: id }, { call, put }) {
      // id为0的时候，即新增用户，则覆盖profile
      let profile = null;
      if (id > 0) {
        const { data } = yield call(usersService.fetch, id);
        profile = data;
      }

      yield put({
        type: 'saveProfile',
        payload: {
          profile,
        },
      });
    },
    *fetchSelfProfile({ payload }, { call, put }) {
      const response = yield call(usersService.fetchProfile);
      const { data: loginedUser } = response;
      yield put({
        type: 'saveLoginedUser',
        payload: {
          loginedUser,
        },
      });
      return response;
    },
    *create(
      {
        payload: { values },
      },
      { call, put, select }
    ) {
      const response = yield call(usersService.create, values);
      const page = yield select(state => state.users.page);
      yield put({ type: 'fetchList', payload: { page } });
      return response;
    },
    *update(
      {
        payload: { id, values },
      },
      { call, put, select }
    ) {
      const response = yield call(usersService.update, id, values);
      const page = yield select(state => state.users.page);
      yield put({ type: 'fetchList', payload: { page } });
      return response;
    },
    *updateProfile(
      {
        payload: { values },
      },
      { call, put, select }
    ) {
      const response = yield call(usersService.updateProfile, values);
      const page = yield select(state => state.users.page);
      yield put({ type: 'fetchList', payload: { page } });
      return response;
    },
    *updateStatus(
      {
        payload: { id, status },
      },
      { call, put, select }
    ) {
      yield call(usersService.updateStatus, id, status);
      const page = yield select(state => state.users.page);
      yield put({ type: 'fetchList', payload: { page } });
    },
    *updatePassword(
      {
        payload: { id, password },
      },
      { call, put, select }
    ) {
      const response = yield call(usersService.updatePassword, id, password);
      const page = yield select(state => state.users.page);
      yield put({ type: 'fetchList', payload: { page } });
      return response;
    },
    *updateSelfPassword(
      {
        payload: { password },
      },
      { call, put, select }
    ) {
      const response = yield call(usersService.updateSelfPassword, password);
      const page = yield select(state => state.users.page);
      yield put({ type: 'fetchList', payload: { page } });
      return response;
    },
    *fetchRoles(payload, { call, put }) {
      const { data: roles } = yield call(usersService.fetchRoles);
      yield put({
        type: 'saveRoles',
        payload: {
          roles,
        },
      });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/users') {
          dispatch({ type: 'fetchList', payload: query });
        }
      });
    },
  },
};
